package cn.dubnation.base.service.impl;

import cn.dubnation.base.mapper.BaseMapper;
import cn.dubnation.base.query.BaseQuery;
import cn.dubnation.base.service.IBaseService;
import cn.dubnation.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired(required = false)
    private BaseMapper<T> baseMapper ;

    @Transactional
    @Override
    public void insert(T t) {
        baseMapper.insert(t);
    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Transactional
    @Override
    public void delete(Serializable id) {
        baseMapper.delete(id);
    }

    @Override
    public List<T> queryAll() {
        return baseMapper.queryAll();
    }

    @Override
    public T queryById(Serializable id) {
        return baseMapper.queryById(id);
    }

    @Override
    public PageInfo<T> queryPage(BaseQuery baseQuery) {
        //查询总条数
        Long dataNums = baseMapper.queryTotal(baseQuery);
        if (dataNums != 0){  //先查询数据库是否有数据，有数据再查
            //查询数据
            List<T> departments =  baseMapper.queryData(baseQuery);
            return new PageInfo<T>(dataNums, departments);
        }
        //没有数据，返回一个空对象
        return new PageInfo<T>();
    }

    @Override
    public void batchDelete(List<Long> ids) {
        baseMapper.batchDelete(ids);
    }
}
